<?php
class MenuHelper extends AppHelper {
    
    var $defaults = array(
        'submenu' => null,
        'url' => null,
        'alias' => null,
        'access' => false,
    );
    
    function build($menus,$for='admin') {
        $output = '';
        foreach($menus as $id => $menu):
            if($for=='admin')
                $output .= $this->buildMenuAdmin($menu, $id);
            else $output .= $this->buildMenuPublic($menu, $id);
        endforeach;
        
        return $output;
    }
    
    function buildMenuAdmin($menu, $id) {
        
        $menu = am($this->defaults, $menu);
        
        
        $id = "id='$id' ";
        $output = '';
        
        
        
        if ($menu['url'] === null) return;
        $menu['url'] = $this->url($menu['url']);
        
        $hasSubMenu = is_array($menu['submenu']);
        
        $currentParent = '';
        if($this->here == $menu['url']) :
            $currentParent = 'selected ';
        else :
            if(is_array($menu['alias'])) :
                foreach($menu['alias'] as $alias) :
                    if(strpos($this->here, $this->url($alias)) !== false) :
                        $currentParent = 'selected ';
                        break;
                    endif;
                endforeach;
            endif;
        endif;
        
            if($hasSubMenu) :
                $output .= "
                
                        <ul class='submenu'>";
                
                foreach($menu['submenu'] as $key => $subMenu):
                
                    $subMenu = am($this->defaults, $subMenu);
                    
                    $subExtra = '';
                    if($key == 0) $subExtra = "first-item ";
                    
                    $subMenu['url'] = $this->url($subMenu['url']);
                    
                    $current = '';
                    if($this->here == $subMenu['url']) :
                        $current = 'current';
                    else:
                        if(is_array($subMenu['alias'])) :
                            foreach($subMenu['alias'] as $alias) :
                                if(strpos($this->here, $this->url($alias)) !== false) :
                                    $current = 'current';
                                    break;
                                endif;
                            endforeach;
                        endif;
                    endif;
                    
                    if($subExtra || $current)$subExtra = "class='{$subExtra}{$current}'";
                        
                    
                    if($current)$currentParent='selected ';
                    if($subMenu['access'])
                        $output .= "<li {$subExtra}><a href='{$subMenu['url']}' {$subExtra}>{$subMenu['title']}</a></li>".PHP_EOL;
                endforeach;
                        
                $output .= "<li class='last'></li></ul>
                ";
            endif;
        if($menu['access'])
            $output = "<li {$id}  class='{$currentParent}'>".($hasSubMenu?'<div class="menu-toggle"><br/></div>':'')."<a class='".($currentParent?'expanded':'')."' href='{$menu['url']}' title='{$menu['title']}'>{$menu['title']}</a>{$output}</li>";
    
        
        
        return $output;
    }
    
    
    
    /*  same as admin menu builder only the html formating is different   */
    
    
    function buildMenuPublic($menu, $id) {
        
        $menu = am($this->defaults, $menu);
        
        
        $id = "id='$id' ";
        $output = '';
        
        
        
        if ($menu['url'] === null) return;
        $menu['url'] = $this->url($menu['url']);
        
        $hasSubMenu = is_array($menu['submenu']);
        
        $currentParent = '';
        if($this->here == $menu['url']) :
            $currentParent = 'selected ';
        else :
            if(is_array($menu['alias'])) :
                foreach($menu['alias'] as $alias) :
                    if(strpos($this->here, $this->url($alias)) !== false) :
                        $currentParent = 'selected ';
                        break;
                    endif;
                endforeach;
            endif;
        endif;
        
            if($hasSubMenu) :
                $output .= "
                
                        <ul class='submenu'>";
                
                foreach($menu['submenu'] as $key => $subMenu):
                
                    $subMenu = am($this->defaults, $subMenu);
                    
                    $subExtra = '';
                    if($key == 0) $subExtra = "first-item ";
                    
                    $subMenu['url'] = $this->url($subMenu['url']);
                    
                    $current = '';
                    if($this->here == $subMenu['url']) :
                        $current = 'current';
                    else:
                        if(is_array($subMenu['alias'])) :
                            foreach($subMenu['alias'] as $alias) :
                                if(strpos($this->here, $this->url($alias)) !== false) :
                                    $current = 'current';
                                    break;
                                endif;
                            endforeach;
                        endif;
                    endif;
                    
                    if($subExtra || $current)$subExtra = "class='{$subExtra}{$current}'";
                        
                    
                    if($current)$currentParent='selected ';
                    if($subMenu['access'])
                        $output .= "<li {$subExtra}><a href='{$subMenu['url']}' {$subExtra} >{$subMenu['title']}</a></li>".PHP_EOL;
                endforeach;
                        
                $output .= "</ul>
                ";
            endif;
        if($menu['access'])
            $output = "<li {$id}  class='{$currentParent} ".($hasSubMenu?'sub-list':'')."'>"."<a id='".trim($currentParent)."' class='".($currentParent?'expanded':'')."' href='{$menu['url']}' title='{$menu['title']}'>{$menu['title']}</a>{$output}</li>";
    
        
        
        return $output;
    }
    
}
?>